﻿using NfsBLL;
using NfsCommon.BaseSoa;
using NfsCommon.Util;
using NfsModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ManagerWed.Controllers
{
    [BaseController(IsCheck = false)]
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        public ActionResult Login()
        {
            if (CookieHelper.GetValue("pcnfs_UserId") == null || CookieHelper.GetValue("pcnfs_UserId") == "")
            {
                return View();
            }
            else {
                return Redirect("/Account/Index"); 
            }
           
         
        }
        #region 登录
        [HttpPost]
        public JsonResult GetLogin(string userName, string userPwd)
        {
            try
            {
                AccountBLL bll = new AccountBLL();
                account model = bll.GetLogin(userName, userPwd);
                Responses re = new Responses();
                if (model == null)
                {
                   
                    return Json("", JsonRequestBehavior.AllowGet);
                }
                else
                {
                    CookieHelper.Save("pcnfs_UserId", Encrypt.Encode(model.id.ToString()), 3);
                    CookieHelper.Save("pcnfs_user_name", Encrypt.Encode(model.user_name.ToString()), 3);
                    CookieHelper.Save("pcnfs_type", Encrypt.Encode(model.type.ToString()), 3);
                    CookieHelper.Save("pcnfs_IsAdmin", Encrypt.Encode(model.IsAdmin.ToString()), 3);
                    return Json(model, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception)
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }

        }
        #endregion

        /// <summary>
        /// 退出时清楚Cookie
        /// </summary>
        /// <returns></returns>
        public ActionResult ClearSession()
        {

            CookieHelper.Remove("pcnfs_UserId");
            CookieHelper.Remove("pcnfs_user_name");
            CookieHelper.Remove("pcnfs_type");
            CookieHelper.Remove("pcnfs_IsAdmin");
            return Redirect("/Login/Login");
        }
    }
}